
#ifndef __CONNECTOR_HPP__
#define __CONNECTOR_HPP__ 

#include "khan.hpp"

namespace khan {

using boost::asio::ip::tcp;

template <typename SessionPtr, typename Manager>
class connector
: private boost::noncopyable
{
public:
    connector(boost::asio::io_service& io_service,
            const tcp::endpoint& ep, Manager& manager)
        : _ioserv(io_service),
        _manager(manager)
    {
        SessionPtr new_session = 
            _manager.create_session(_ioserv);
        new_session->socket().async_connect(ep,
                boost::bind(&connector::handle_connect, this, 
                    new_session,
                    boost::asio::placeholders::error));

    }
private:
    void handle_connect(SessionPtr new_session, const boost::system::error_code& error)
    {
        if (!error) {
            new_session->start();
        }
        else {
            PDBG("exception: %s\n", error.message().c_str());
            new_session->stop();
        }
    }
    boost::asio::io_service& _ioserv;
    Manager& _manager;
};

}

#endif 
